<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveSupport::Multibyte::Chars</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveSupport::Multibyte::Chars</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/activesupport/lib/active_support/multibyte/chars_rb.html">
                vendor/rails/activesupport/lib/active_support/multibyte/chars.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
<a href="Chars.html">Chars</a> enables you to work transparently with
multibyte encodings in the Ruby String class without having extensive
knowledge about the encoding. A <a href="Chars.html">Chars</a> object
accepts a string upon initialization and proxies String methods in an
encoding safe manner. All the normal String methods are also implemented on
the proxy.
</p>
<p>
String methods are proxied through the <a href="Chars.html">Chars</a>
object, and can be accessed through the <tt>chars</tt> method. Methods
which would normally return a String object now return a <a
href="Chars.html">Chars</a> object so methods can be chained.
</p>
<pre>
  &quot;The Perfect String  &quot;.chars.downcase.strip.normalize #=&gt; &quot;the perfect string&quot;
</pre>
<p>
<a href="Chars.html">Chars</a> objects are perfectly interchangeable with
String objects as long as no explicit class checks are made. If certain
methods do explicitly check the class, call <tt>to_s</tt> before you pass
chars objects to them.
</p>
<pre>
  bad.explicit_checking_method &quot;T&quot;.chars.downcase.to_s
</pre>
<p>
The actual operations on the string are delegated to handlers.
Theoretically handlers can be implemented for any encoding, but the default
<a href="Chars.html#M000556">handler</a> handles UTF-8. This <a
href="Chars.html#M000556">handler</a> is set during initialization, if you
want to use you own <a href="Chars.html#M000556">handler</a>, you can set
it on the <a href="Chars.html">Chars</a> class. Look at the UTF8Handler
source for an example how to implement your own <a
href="Chars.html#M000556">handler</a>. If you your own <a
href="Chars.html#M000556">handler</a> to work on anything but UTF-8 you
probably also want to override <a
href="Chars.html#M000556">Chars#handler</a>.
</p>
<pre>
  ActiveSupport::Multibyte::Chars.handler = MyHandler
</pre>
<p>
Note that a few methods are defined on <a href="Chars.html">Chars</a>
instead of the <a href="Chars.html#M000556">handler</a> because they are
defined on <a href="../../Object.html">Object</a> or <a
href="../../Kernel.html">Kernel</a> and <a
href="Chars.html#M000554">method_missing</a> can&#8216;t catch them.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000550">&lt;=&gt;</a>&nbsp;&nbsp;
      <a href="#M000553">=~</a>&nbsp;&nbsp;
      <a href="#M000552">gsub</a>&nbsp;&nbsp;
      <a href="#M000556">handler</a>&nbsp;&nbsp;
      <a href="#M000555">handler=</a>&nbsp;&nbsp;
      <a href="#M000554">method_missing</a>&nbsp;&nbsp;
      <a href="#M000549">new</a>&nbsp;&nbsp;
      <a href="#M000548">respond_to?</a>&nbsp;&nbsp;
      <a href="#M000551">split</a>&nbsp;&nbsp;
      <a href="#M000547">to_str</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name">Comparable</span>
      </div>
    </div>

    <div id="section">



    <div id="aliases-list">
      <h3 class="section-bar">External Aliases</h3>

      <div class="name-list">
                        <table summary="aliases">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">string</td>
          <td>-&gt;</td>
          <td class="context-item-value">to_s</td>
        </tr>
                        </table>
      </div>
    </div>


    <div id="attribute-list">
      <h3 class="section-bar">Attributes</h3>

      <div class="name-list">
        <table>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">string</td>
          <td class="context-item-value">&nbsp;[R]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        </table>
      </div>
    </div>
      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000555" class="method-detail">
        <a name="M000555"></a>

        <div class="method-heading">
          <a href="#M000555" class="method-signature">
          <span class="method-name">handler=</span><span class="method-args">(klass)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Set the <a href="Chars.html#M000556">handler</a> class for the Char
objects.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000555-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000555-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 104</span>
104:     <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">handler=</span>(<span class="ruby-identifier">klass</span>)
105:       <span class="ruby-ivar">@@handler</span> = <span class="ruby-identifier">klass</span>
106:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000549" class="method-detail">
        <a name="M000549"></a>

        <div class="method-heading">
          <a href="#M000549" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(str)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Create a <a href="Chars.html#M000549">new</a> <a
href="Chars.html">Chars</a> instance.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000549-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000549-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 51</span>
51:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">str</span>)
52:       <span class="ruby-ivar">@string</span> = <span class="ruby-identifier">str</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:string</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">str</span>.<span class="ruby-identifier">string</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">str</span>
53:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000550" class="method-detail">
        <a name="M000550"></a>

        <div class="method-heading">
          <a href="#M000550" class="method-signature">
          <span class="method-name">&lt;=&gt;</span><span class="method-args">(other)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns -1, 0 or +1 depending on whether the <a href="Chars.html">Chars</a>
object is to be sorted before, equal or after the object on the right side
of the operation. It accepts any object that implements <tt>to_s</tt>. See
String.&lt;=&gt; for more details.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000550-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000550-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 58</span>
58:     <span class="ruby-keyword kw">def</span> <span class="ruby-operator">&lt;=&gt;</span>(<span class="ruby-identifier">other</span>); <span class="ruby-ivar">@string</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">to_s</span>; <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000553" class="method-detail">
        <a name="M000553"></a>

        <div class="method-heading">
          <a href="#M000553" class="method-signature">
          <span class="method-name">=~</span><span class="method-args">(other)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Like String.=~ only it returns the character offset (in codepoints) instead
of the byte offset.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000553-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000553-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 70</span>
70:     <span class="ruby-keyword kw">def</span> <span class="ruby-operator">=~</span>(<span class="ruby-identifier">other</span>)
71:       <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">translate_offset</span>(<span class="ruby-ivar">@string</span>, <span class="ruby-ivar">@string</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">other</span>)
72:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000552" class="method-detail">
        <a name="M000552"></a>

        <div class="method-heading">
          <a href="#M000552" class="method-signature">
          <span class="method-name">gsub</span><span class="method-args">(*a, &amp;b)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Gsub works exactly the same as <a href="Chars.html#M000552">gsub</a> on a
normal string.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000552-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000552-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 67</span>
67:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">gsub</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>); <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>).<span class="ruby-identifier">chars</span>; <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000556" class="method-detail">
        <a name="M000556"></a>

        <div class="method-heading">
          <a href="#M000556" class="method-signature">
          <span class="method-name">handler</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the proper <a href="Chars.html#M000556">handler</a> for the
contained string depending on $KCODE and the encoding of the string. This
method is used internally to always redirect messages to the proper classes
depending on the context.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000556-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000556-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 110</span>
110:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">handler</span>
111:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">utf8_pragma?</span>
112:         <span class="ruby-ivar">@@handler</span>
113:       <span class="ruby-keyword kw">else</span>
114:         <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Multibyte</span><span class="ruby-operator">::</span><span class="ruby-constant">Handlers</span><span class="ruby-operator">::</span><span class="ruby-constant">PassthruHandler</span>
115:       <span class="ruby-keyword kw">end</span>
116:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000554" class="method-detail">
        <a name="M000554"></a>

        <div class="method-heading">
          <a href="#M000554" class="method-signature">
          <span class="method-name">method_missing</span><span class="method-args">(m, *a, &amp;b)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Try to forward all undefined methods to the <a
href="Chars.html#M000556">handler</a>, when a method is not defined on the
<a href="Chars.html#M000556">handler</a>, send it to the contained string.
Method_missing is also responsible for making the bang! methods
destructive.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000554-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000554-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 76</span>
 76:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">method_missing</span>(<span class="ruby-identifier">m</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
 77:       <span class="ruby-keyword kw">begin</span>
 78:         <span class="ruby-comment cmt"># Simulate methods with a ! at the end because we can't touch the enclosed string from the handlers.</span>
 79:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/^(.*)\!$/</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">$1</span>)
 80:           <span class="ruby-identifier">result</span> = <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">$1</span>, <span class="ruby-ivar">@string</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
 81:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@string</span>
 82:             <span class="ruby-identifier">result</span> = <span class="ruby-keyword kw">nil</span>
 83:           <span class="ruby-keyword kw">else</span>
 84:             <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">replace</span> <span class="ruby-identifier">result</span>
 85:           <span class="ruby-keyword kw">end</span>
 86:         <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">m</span>)
 87:           <span class="ruby-identifier">result</span> = <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">m</span>, <span class="ruby-ivar">@string</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
 88:         <span class="ruby-keyword kw">else</span>
 89:           <span class="ruby-identifier">result</span> = <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">m</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
 90:         <span class="ruby-keyword kw">end</span>
 91:       <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Handlers</span><span class="ruby-operator">::</span><span class="ruby-constant">EncodingError</span>
 92:         <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">replace</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">tidy_bytes</span>(<span class="ruby-ivar">@string</span>)
 93:         <span class="ruby-keyword kw">retry</span>
 94:       <span class="ruby-keyword kw">end</span>
 95:       
 96:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">String</span>)
 97:         <span class="ruby-identifier">result</span>.<span class="ruby-identifier">chars</span>
 98:       <span class="ruby-keyword kw">else</span>
 99:         <span class="ruby-identifier">result</span>
100:       <span class="ruby-keyword kw">end</span>
101:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000548" class="method-detail">
        <a name="M000548"></a>

        <div class="method-heading">
          <a href="#M000548" class="method-signature">
          <span class="method-name">respond_to?</span><span class="method-args">(method)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Make duck-typing with String possible
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000548-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000548-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 45</span>
45:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">method</span>)
46:       <span class="ruby-keyword kw">super</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">method</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">method</span>) <span class="ruby-operator">||</span>
47:         (<span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/(.*)!/</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">handler</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">$1</span>)) <span class="ruby-operator">||</span> <span class="ruby-keyword kw">false</span>
48:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000551" class="method-detail">
        <a name="M000551"></a>

        <div class="method-heading">
          <a href="#M000551" class="method-signature">
          <span class="method-name">split</span><span class="method-args">(*args)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Works just like String#split, with the exception that the items in the
resulting list are <a href="Chars.html">Chars</a> instances instead of
String. This makes chaining methods easier.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000551-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000551-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 62</span>
62:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">split</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
63:       <span class="ruby-ivar">@string</span>.<span class="ruby-identifier">split</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>).<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">chars</span> }
64:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000547" class="method-detail">
        <a name="M000547"></a>

        <div class="method-heading">
          <a href="#M000547" class="method-signature">
          <span class="method-name">to_str</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
The magic method to make String and <a href="Chars.html">Chars</a>
comparable
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000547-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000547-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/multibyte/chars.rb, line 38</span>
38:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_str</span>
39:       <span class="ruby-comment cmt"># Using any other ways of overriding the String itself will lead you all the way from infinite loops to</span>
40:       <span class="ruby-comment cmt"># core dumps. Don't go there.</span>
41:       <span class="ruby-ivar">@string</span>
42:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>